from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeSolid
from OCC.Core.BRepExtrema import BRepExtrema_DistShapeShape
from OCC.Core.TopAbs import TopAbs_SHELL
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.gp import gp_Pnt
from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_stl_file
from OCC.Extend.ShapeFactory import make_edge, make_vertex

shapes = read_stl_file('../09_load_model/ceshi2.STL')

explorer = TopExp_Explorer(shapes, TopAbs_SHELL)

boxes = []

while explorer.More():
    shell = explorer.Current()
    ms = BRepBuilderAPI_MakeSolid()
    ms.Add(shell)
    boxes.append(ms.Solid())
    explorer.Next()

pnt = gp_Pnt(85, -10, 123)

dss = BRepExtrema_DistShapeShape()
dss.LoadS1(boxes[0])
dss.LoadS2(make_vertex(pnt))
dss.Perform()

assert dss.IsDone()

print("Minimal distance between cubes: ", dss.Value())
edg = make_edge(dss.PointOnShape1(1), dss.PointOnShape2(1))

display, start_display, add_menu, add_function_to_menu = init_display()

display.DisplayShape(pnt)
display.DisplayShape(boxes)
display.DisplayColoredShape(edg, color='CYAN')

start_display()
